package com.supor.aiot.food.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.RecipeHotBean;
import com.supor.aiot.food.bean.SearchRecommandBean;

import java.util.List;


public class SearchRecommondAdapter extends RecyclerView.Adapter<SearchRecommondAdapter.RecipeDeviceHolder>{
    private List<RecipeHotBean> mList;
    private Context mContext;
    private onItemClickListener recipeDeviceClick;
    private boolean showDate;

    public SearchRecommondAdapter(List<RecipeHotBean> mList, Context mContext , onItemClickListener listener) {
        this.mList = mList;
        this.mContext = mContext;
        this.recipeDeviceClick = listener;
    }

    public void setShowDate(boolean show){
        this.showDate = show;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SearchRecommondAdapter.RecipeDeviceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_search_recomond,parent, false);
        return new SearchRecommondAdapter.RecipeDeviceHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeDeviceHolder holder, int position) {
        RecipeHotBean hotBean = mList.get(position);

        holder.llSearchOuter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recipeDeviceClick.OnItemClicked(position);
            }
        });
        if (position==0){
            holder.ivRank.setVisibility(View.VISIBLE);
            holder.tvSearchCount.setVisibility(View.GONE);
            holder.ivRank.setImageResource(R.mipmap.rank_one);
        }else if(position==1){
            holder.ivRank.setVisibility(View.VISIBLE);
            holder.tvSearchCount.setVisibility(View.GONE);
            holder.ivRank.setImageResource(R.mipmap.rank_two);
        }else if(position==2){
            holder.ivRank.setVisibility(View.VISIBLE);
            holder.tvSearchCount.setVisibility(View.GONE);
            holder.ivRank.setImageResource(R.mipmap.rank_three);
        }else {
            holder.ivRank.setVisibility(View.GONE);
            holder.tvSearchCount.setVisibility(View.VISIBLE);
            holder.tvSearchCount.setText(position+1+"");
        }

        if (!TextUtils.isEmpty(hotBean.getRecipeName()))holder.tvSearchTitle.setText(hotBean.getRecipeName());
        if (showDate){
            String time = hotBean.getReleaseTime();
            if (!TextUtils.isEmpty(time)){
                simplify(holder,time);
            }else {
                holder.tvCookCount.setText(hotBean.getReleaseTime());
            }
            if (position==0){
                holder.tvTitleTag.setVisibility(View.VISIBLE);
                holder.tvTitleTag.setText(mContext.getResources().getString(R.string.upload_time));
            }else {
                holder.tvTitleTag.setVisibility(View.GONE);
            }
        }else {
            holder.tvCookCount.setText(""+hotBean.getPrCount());
            if (position==0){
                holder.tvTitleTag.setVisibility(View.VISIBLE);
                holder.tvTitleTag.setText(mContext.getResources().getString(R.string.cook_count));
            }else {
                holder.tvTitleTag.setVisibility(View.GONE);
            }
        }
        if (!TextUtils.isEmpty(hotBean.getCrmModelName()))holder.tvCookDevice.setText(hotBean.getCrmModelName());
    }

    private void simplify(RecipeDeviceHolder holder,String date){
        int index1 =date.indexOf("-");
        int index2 =date.lastIndexOf("-");
        int month;
        int day;
        int year=Integer.parseInt(date.substring(0,index1));
        if(date.substring(index1+1,index1+2).equals("0")){
            month=Integer.parseInt(date.substring(index1+2,index1+3));
        }else {
            month=Integer.parseInt(date.substring(index1+1,index1+3));
        }
        if(date.substring(index2+1,index2+2).equals("0")){
            day=Integer.parseInt(date.substring(index2+2,index2+3));
        }else{
            day=Integer.parseInt(date.substring(index2+1,index2+3));
        }

        holder.tvCookCount.setText(month+mContext.getResources().getString(R.string.month)+day+mContext.getResources().getString(R.string.day));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class RecipeDeviceHolder extends RecyclerView.ViewHolder{
        public LinearLayout llSearchOuter;
        public ImageView ivRank;
        public TextView tvSearchCount;
        public TextView tvSearchTitle;
        public TextView tvCookCount;
        public TextView tvCookDevice;
        public TextView tvTitleTag;

        public RecipeDeviceHolder(@NonNull View itemView) {
            super(itemView);
            llSearchOuter=itemView.findViewById(R.id.ll_search_outer);
            ivRank=itemView.findViewById(R.id.iv_search_rank);
            tvSearchCount=itemView.findViewById(R.id.tv_search_count);
            tvSearchTitle=itemView.findViewById(R.id.tv_search_title);
            tvCookCount=itemView.findViewById(R.id.tv_cooker_count);
            tvCookDevice=itemView.findViewById(R.id.tv_cooker_device);
            tvTitleTag=itemView.findViewById(R.id.tv_title_tag);
        }
    }

    public interface onItemClickListener {
        void OnItemClicked(int position);
    }

}
